package com.road.controller;

import com.road.model.easyui.Tree;
import com.road.model.entity.Resource;
import com.road.service.ResourceService;
import com.road.utils.MessageModel;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;

/**
 * @description：资源管理
 */
@Controller
@RequestMapping("/system/resource")
public class ResourceController extends BaseController {

	private static Logger logger = LoggerFactory.getLogger(ResourceController.class);

	@Autowired
	private ResourceService resourceService;

	/**
	 * 
	 * @Name: index
	 * @Description:菜单树
	 * @Author: WeiHui.Zhang
	 * @Version: V1.00
	 * @Date: 2016年3月17日下午2:34:43
	 * @return
	 * @throws Exception
	 * @Return: List<Resource>
	 */
	@RequestMapping(method = RequestMethod.POST)
	@ResponseBody
	public List<Resource> index() throws Exception {
		Long userId = getCurrentUserId();
		if (userId != null) {
			List<Resource> resources = resourceService.findResourceByUserId(getCurrentUserId());
			return resources;
		}
		throw new IllegalArgumentException("");
	}

	/**
	 * 资源管理页
	 *
	 * @return
	 */
	@RequestMapping(value = "/manager", method = RequestMethod.GET)
	public String manager() {
		return "system/resourceList";
	}

	/**
	 * 资源管理列表
	 *
	 * @return
	 */
	@RequestMapping(value = "/treeGrid", method = RequestMethod.POST)
	@ResponseBody
	public List<Resource> treeGrid() {
		List<Resource> treeGrid = resourceService.findResourceAll();
		return treeGrid;
	}

	/**
	 * 
	 * @Name: menu
	 * @Description:资源菜单管理
	 * @Author: WeiHui.Zhang
	 * @Version: V1.00
	 * @Date: 2016年3月17日下午2:29:29
	 * @return
	 * @Return: String
	 */
	@RequestMapping(value = "/menu", method = RequestMethod.GET)
	public String menu() {
		return "system/menuList";
	}

	/**
	 * 菜单集合(JSON)
	 */
	//@RequiresPermissions("sys:perm:menu:view")
	@RequestMapping(value = "menu/json", method = RequestMethod.GET)
	@ResponseBody
	public List<Resource> menuDate() {
		List<Resource> resources = resourceService.getMenus();
		return resources;
	}
	
	/**
	 * 修改菜单跳转
	 */
	@RequestMapping(value = "menu/update/{id}", method = RequestMethod.GET)
	public String updateMenuForm(@PathVariable("id") Long id, Model model) {
		model.addAttribute("resource", resourceService.findResourceById(id));
		model.addAttribute("action", "update");
		return "system/menuForm";
	}


	@RequestMapping(value = "menu/update", method = RequestMethod.POST)
	@ResponseBody
	public String updateMenuResource(Resource resource) {
        Resource res = resourceService.findResourceById(resource.getId());
        res.setName(resource.getName());
        res.setUrl(resource.getUrl());
        res.setIcon(resource.getIcon());
        res.setSeq(resource.getSeq());
        res.setDescription(resource.getDescription());
        res.setUpdateDate(new Date());
        res.setUpdateUser(getCurrentUser());
        resourceService.updateResource(res);
		return "true";
	}

	/**
	 * 添加资源页
	 *
	 * @return
	 */
	@RequestMapping("/addPage")
	public String addPage() {
		return "/admin/resourceAdd";
	}

	/**
	 * 添加资源
	 *
	 * @param resource
	 * @return
	 */
	@RequestMapping("/add")
	@ResponseBody
	public MessageModel add(Resource resource) {
		MessageModel result = new MessageModel();
		try {
			resourceService.addResource(resource);
			result.setSuccess(true);
			result.setMsg("添加成功！");
			return result;
		} catch (RuntimeException e) {
			logger.error("添加资源失败：{}", e);
			result.setMsg(e.getMessage());
			return result;
		}
	}

	/**
	 * 二级资源树
	 *
	 * @return
	 */
	@RequestMapping("/allTree")
	@ResponseBody
	public List<Tree> allTree() {
		return resourceService.findAllTree();
	}

	/**
	 * 三级资源树
	 *
	 * @return
	 */
	@RequestMapping(value = "/allTrees", method = RequestMethod.POST)
	@ResponseBody
	public List<Tree> allTrees() {
		return resourceService.findAllTrees();
	}

	/**
	 * 编辑资源页
	 *
	 * @param request
	 * @param id
	 * @return
	 */
	@RequestMapping("/editPage")
	public String editPage(HttpServletRequest request, Long id) {
		Resource resource = resourceService.findResourceById(id);
		request.setAttribute("resource", resource);
		return "/admin/resourceEdit";
	}

	/**
	 * 编辑资源
	 *
	 * @param resource
	 * @return
	 */
	@RequestMapping("/edit")
	@ResponseBody
	public MessageModel edit(Resource resource) {
		MessageModel result = new MessageModel();
		try {
			resourceService.updateResource(resource);
			result.setSuccess(true);
			result.setMsg("编辑成功！");
			return result;
		} catch (RuntimeException e) {
			logger.error("编辑资源失败：{}", e);
			result.setMsg(e.getMessage());
			return result;
		}
	}

	/**
	 * 删除资源
	 *
	 * @param id
	 * @return
	 */
	@RequestMapping("/delete")
	@ResponseBody
	public MessageModel delete(Long id) {
		MessageModel result = new MessageModel();
		try {
			resourceService.deleteResourceById(id);
			result.setMsg("删除成功！");
			result.setSuccess(true);
			return result;
		} catch (RuntimeException e) {
			logger.error("删除资源失败：{}", e);
			result.setMsg(e.getMessage());
			return result;
		}
	}

}
